<?php
 class Videos extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){
        $this->db->order_by('CreatedDate DESC');
        $query = $this->db->get('videos');
        $res = $query->result_array();
        return $res;
    }
    
    public function getOptions(){
        $this->db->select('videos.VideoId, videos.VideoName');
        $this->db->from('videos');
        $this->db->where('videos.Visible', 1);
        $this->db->order_by('videos.VideoName');
        $query = $this->db->get();
       $list =  $query->result();
       $res = array();
       
       foreach($list as $item){
            $res[$item->VideoId] = $item->VideoName;
       }
       return $res;
    }
        
    /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getVideo($videoId){
        $query = $this->db->get_where('videos', array('VideoId' => $videoId));
        return $query->row_array();
    }
    
    /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $videoId){
        $data = $this->_convertData($data);
        return $this->db->update('videos', $data, array('VideoId' => $videoId));
    }
    
    /**
     * Delete product id
     * @author tien.nguyen
     */
    public function delete($videoId){
       return $this->db->delete('videos', array('VideoId' => $videoId)); 
    }
    
    /**
     * add product
     * @author tien.nguyen
     */
    public function add($data){
        $data = $this->_convertData($data);
        $data['CreatedDate'] = date('Y-m-d H:m:s');
        return $this->db->insert('videos', $data);
    }
    
    /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('VideoId', 'VideoName', 'Embed', 
            'CreatedDate', 'Visible');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        
        //set default value
        $res['Visible'] = isset($res['Visible']) ? $res['Visible'] : 0;
        return $res;
    }
 }
?>